package com.contain.minio.service;

import io.minio.GetObjectArgs;
import io.minio.GetObjectResponse;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.errors.*;
import okhttp3.Headers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: tianchao
 * @Date: 2021/8/21 23:37
 * @Description:
 */
@Service
public class ObjectOperationsService {
    @Autowired
    private MinioClient minioClient;

    public void getObject(){
        try {
            GetObjectResponse dockerminio = minioClient.getObject(GetObjectArgs.builder().bucket("dockerminio").object("buffer.txt").build());
            Headers headers = dockerminio.headers();
            System.out.println(headers.toString());
            // Read the input stream and print to the console till EOF.
            byte[] buf = new byte[16384];
            int bytesRead;
            while ((bytesRead = dockerminio.read(buf, 0, buf.length)) >= 0) {
                System.out.println(new String(buf, 0, bytesRead, StandardCharsets.UTF_8));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void putObject(){
        StringBuilder builder = new StringBuilder();
        //for (int i = 0; i < 1000; i++) {
            builder.append(
                    "Sphinx of black quartz, judge my vow: Used by Adobe InDesign to display font samples. ");
            builder.append("(29 letters)\n");
            builder.append(
                    "Jackdaws love my big sphinx of quartz: Similarly, used by Windows XP for some fonts. ");
            builder.append("(31 letters)\n");
            builder.append(
                    "Pack my box with five dozen liquor jugs: According to Wikipedia, this one is used on ");
            builder.append("NASAs Space Shuttle. (32 letters)\n");
            builder.append(
                    "The quick onyx goblin jumps over the lazy dwarf: Flavor text from an Unhinged Magic Card. ");
            builder.append("(39 letters)\n");
            builder.append(
                    "How razorback-jumping frogs can level six piqued gymnasts!: Not going to win any brevity ");
            builder.append("awards at 49 letters long, but old-time Mac users may recognize it.\n");
            builder.append(
                    "Cozy lummox gives smart squid who asks for job pen: A 41-letter tester sentence for Mac ");
            builder.append("computers after System 7.\n");
            builder.append(
                    "A few others we like: Amazingly few discotheques provide jukeboxes; Now fax quiz Jack! my ");
            builder.append("brave ghost pled; Watch Jeopardy!, Alex Trebeks fun TV quiz game.\n");
            builder.append("---\n");
        //}

        {
            Map<String,String> map = new HashMap<>();
            map.put("a", "b");
            map.put("c", "d");

            // Create object 'my-objectname' in 'my-bucketname' with content from the input stream.
            try {
                // Create a InputStream for object upload.
                ByteArrayInputStream bais = new ByteArrayInputStream(builder.toString().getBytes("UTF-8"));
                minioClient.putObject(
                        PutObjectArgs.builder().bucket("dockerminio").object("buffer.txt").userMetadata(map).stream(
                                bais, bais.available(), -1)
                                .build());
                bais.close();
                System.out.println("my-objectname is uploaded successfully");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}
